package github.kawaiior.yuanchuxiuzhen.core.init.event;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.util.ManaPowerHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.ai.attributes.ModifiableAttributeInstance;
import net.minecraft.entity.monster.*;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.commons.lang3.StringUtils;

@Mod.EventBusSubscriber(modid = Yuanchuxiuzhen.MODID)
public class EntitySpawnEvent {
    public static void BiomeLoading(BiomeLoadingEvent event){
//        if( event.getName() == null){
//            return;
//        }
//        MobSpawnInfoBuilder spawns = event.getSpawns();
//
//        if(event.getCategory().equals(Biome.Category.NETHER)){
//            spawns.withSpawner(EntityClassification.MONSTER,new MobSpawnInfo.Spawners(ExampleEntity.TYPE,10,4,4));
//        }
//
//        if (event.getCategory().equals(BiomeInit.HEAVEN)){
//            // 通过配置JSON实现
//            spawns.withSpawner(EntityClassification.MONSTER, new MobSpawnInfo.Spawners(SoulIronBeastEntity.TYPE, 10, 1, 3));
//            spawns.withSpawner(EntityClassification.MONSTER, new MobSpawnInfo.Spawners(CoffinGuardEntity.TYPE, 20, 4, 12));
//            spawns.withSpawner(EntityClassification.MONSTER, new MobSpawnInfo.Spawners(SoulWanderingEntity.TYPE, 30, 4, 12));
//        }
    }

    public static void renameEntity(Entity entity, int multiple, String oldName){
        entity.setCustomName(new StringTextComponent(multiple+"级"+oldName));
    }

    @SubscribeEvent
    public static void onEntityJoin(EntityJoinWorldEvent event){
        int multiple = ManaPowerHelper.getAverageLevelMultiple();

        Entity entity = event.getEntity();

        String oldName = entity.getDisplayName().getString();
        if (StringUtils.containsAny(oldName, '级')){
            return;
        }

        if (entity instanceof ZombieEntity){
            ModifiableAttributeInstance healthAttribute = ((ZombieEntity) entity).getAttribute(Attributes.MAX_HEALTH);
            ModifiableAttributeInstance attackAttribute = ((ZombieEntity) entity).getAttribute(Attributes.ATTACK_DAMAGE);
            healthAttribute.setBaseValue(healthAttribute.getValue() * multiple);
            attackAttribute.setBaseValue(healthAttribute.getValue() * multiple);

            renameEntity(entity, multiple, oldName);
        }
        else if (entity instanceof SpiderEntity){
            float healthMultiple = multiple > 4 ? multiple * 1.5F : multiple;
            ModifiableAttributeInstance healthAttribute = ((SpiderEntity) entity).getAttribute(Attributes.MAX_HEALTH);
            ModifiableAttributeInstance attackAttribute = ((SpiderEntity) entity).getAttribute(Attributes.ATTACK_DAMAGE);
            healthAttribute.setBaseValue(healthAttribute.getValue() * healthMultiple);
            attackAttribute.setBaseValue(healthAttribute.getValue() * multiple);

            renameEntity(entity, multiple, oldName);
        }
        else if (entity instanceof EndermanEntity){
            float healthMultiple = multiple > 5 ? multiple * 2F : multiple;
            ModifiableAttributeInstance healthAttribute = ((EndermanEntity) entity).getAttribute(Attributes.MAX_HEALTH);
            ModifiableAttributeInstance attackAttribute = ((EndermanEntity) entity).getAttribute(Attributes.ATTACK_DAMAGE);
            healthAttribute.setBaseValue(healthAttribute.getValue() * healthMultiple);
            attackAttribute.setBaseValue(healthAttribute.getValue() * multiple);

            renameEntity(entity, multiple, oldName);
        }
        else if (entity instanceof AbstractSkeletonEntity){
            float healthMultiple = multiple > 6 ? multiple * 2F : multiple;
            ModifiableAttributeInstance healthAttribute = ((AbstractSkeletonEntity) entity).getAttribute(Attributes.MAX_HEALTH);
            ModifiableAttributeInstance attackAttribute = ((AbstractSkeletonEntity) entity).getAttribute(Attributes.ATTACK_DAMAGE);
            healthAttribute.setBaseValue(healthAttribute.getValue() * healthMultiple);
            attackAttribute.setBaseValue(healthAttribute.getValue() * multiple);

            renameEntity(entity, multiple, oldName);
        }
        else if (entity instanceof CreeperEntity){
            ModifiableAttributeInstance healthAttribute = ((CreeperEntity) entity).getAttribute(Attributes.MAX_HEALTH);
            healthAttribute.setBaseValue(healthAttribute.getValue() * multiple);

            renameEntity(entity, multiple, oldName);
        }

    }
}
